//    Copyright (C) Kherty.  All rights reserved.
using System;

namespace OpenLS.Spreadsheet
{
    public class Error
    {
        public Error(ErrorType type)
        {
            this.type = type;
        }
        private readonly ErrorType type;
        public ErrorType Type
        {
            get { return type; }
        }
        public override bool Equals(object obj)
        {
            var error = obj as Error;
            if (error == null)
                return false;
            return Type == error.Type;
        }
        public override int GetHashCode()
        {
            return Type.GetHashCode();
        }
	
	

        ///<summary>
        /// Get the string code of the Error. For example, '#NUM!'.
        ///</summary>
        public string Code
        {
            get
            {
                switch (type)
                {
                    case ErrorType.Div0: return "#DIV/0!";
                    case ErrorType.NA: return "#N/A";
                    case ErrorType.Name: return "#NAME?";
                    case ErrorType.Num: return "#NUM!";
                    case ErrorType.Ref: return "#REF!";
                    case ErrorType.Null: return "#NULL!";
                    case ErrorType.Value: return "#VALUE!";
                    default: throw new NotSupportedException();
                }
            }
        }
        public override string ToString()
        {
            return Code;
        }

        internal static object CreateRefError(object value)
        {
            return new RefError(value);
        }
        public static Error RefError
        {
            get
            {
                return new RefError(null);
            }
        }

        internal static object Parse(string s)
        {
            switch (s)
            {
                case "#VALUE":
                case "#VALUE!":
                    return Errors.ValueError;
                case "#REF":
                case "#REF!":
                    return (RefError);
                case "#DIV":
                case "#DIV/0!":
                    return (Errors.DivError);
                case "#NA":
                case "#N/A":
                case "#N/A!": //\\remove?
                    return (Errors.NaError);
                case "#NAME":
                case "#NAME?":
                    return (Errors.NameError);
                case "#NUM":
                case "#NUM!":
                    return (Errors.NumError);
                case "#NULL":
                case "#NULL!":
                    return (Errors.NullError);
                //\\todo: rest
            }
            throw new NotSupportedException(s);
        }
    }
}
